package cn.chenfeifx.core.controls;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

public class CFTab {

    private CFTabPane cfTabPane;
    private TabNode tabNode;

    public CFTab(String text) {
        this.tabNode = new TabNode(text);
        closable.addListener((observable, oldValue, newValue) -> {
            tabNode.closeLabel.setManaged(newValue);
            tabNode.closeLabel.setVisible(newValue);
        });
    }

    public CFTab(String text, boolean closable) {
        this(text);
        setClosable(closable);
    }

    public CFTab(String text, boolean closable, Node graphic) {
        this(text, closable);
        setGraphic(graphic);
    }

    // 是否可关闭
    private final BooleanProperty closable = new SimpleBooleanProperty(true);

    public boolean isClosable() {
        return closable.get();
    }

    public BooleanProperty closableProperty() {
        return closable;
    }

    public void setClosable(boolean closable) {
        this.closable.set(closable);
    }

    public TabNode getTabNode() {
        return tabNode;
    }

    public void setCfTabPane(CFTabPane cfTabPane) {
        this.cfTabPane = cfTabPane;
    }

    public void setToggleGroup(ToggleGroup toggleGroup) {
        tabNode.tabButton.setToggleGroup(toggleGroup);
    }

    public void setSelected(boolean val) {
        tabNode.tabButton.setSelected(val);
    }

    public String getText() {
        return tabNode.tabButton.getText();
    }

    public void setGraphic(Node graphic) {
        tabNode.tabButton.setGraphic(graphic);
    }

    public ToggleButton getTabButton() {
        return tabNode.tabButton;
    }

    public boolean isSelected() {
        return tabNode.tabButton.isSelected();
    }

    public class TabNode extends StackPane {

        private final ToggleButton tabButton;
        private final Label closeLabel;

        public TabNode(String text) {
            tabButton = new ToggleButton(text);
            tabButton.getStyleClass().add("tab-button");
            closeLabel = new Label();
            closeLabel.setGraphic(FontIcon.of(AntDesignIconsOutlined.CLOSE));
            StackPane.setAlignment(closeLabel, Pos.CENTER_RIGHT);
            StackPane.setMargin(closeLabel, new Insets(0, 10, 0, 0));
            closeLabel.getStyleClass().add("close-label");
            getChildren().addAll(tabButton, closeLabel);
            setUserData(CFTab.this);
            getStyleClass().add("cf-tab");
            setMaxSize(USE_PREF_SIZE, USE_PREF_SIZE);
            setMinSize(USE_PREF_SIZE, USE_PREF_SIZE);
            //
            tabButton.setOnAction(event -> tabButton.setSelected(true));
            tabButton.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
                if (!isClosable()) {
                    return;
                }
                if (KeyCode.BACK_SPACE.equals(event.getCode()) || KeyCode.DELETE.equals(event.getCode())) {
                    removeCFTab();
                }
            });
            closeLabel.setOnMouseClicked(event -> removeCFTab());

        }

        /**
         * 从CFTabPane删除CFTab
         */
        private void removeCFTab() {
            if (cfTabPane != null) {
                cfTabPane.getTabs().remove(CFTab.this);
            }
        }
    }

}
